package controllers;

import java.util.Date;
import java.util.List;

import models.ConstructionProject;
import models.Dept;
import models.ProjectVisualizeReport;
import models.Role;
import play.mvc.Before;
import utils.YUtils;

/**
 * verify ConstructionProject
 * @author Administrator
 *
 */
public class VerifyConstProjs extends Application{

	private static List<String> roleIds;
	
	/**
	 * 审核首页
	 */
	public static void index(){
		render();
	}
	
	/**
	 * 获得需审核的基建项目工程
	 */
	public static void verifyCPJson(int page,int pagesize){
		Dept d = connectedUser().dept;
		if(d == null || d.type==null){
			forbidden();
		}
		List<ConstructionProject> constPors = null;
		long total=0;
		//地电分公司
		if(d.type==1){
			constPors = ConstructionProject.find("belongDept=?", d)
					.fetch(page, pagesize);
			total = ConstructionProject.count("belongDept=?", d);
		}
		//地电本部
		if(d.type==2){
			constPors = ConstructionProject.find("verifyState=-2 or verifyState>=1")
					.fetch(page, pagesize);
			total = ConstructionProject.count("verifyState=-2 or verifyState>=1");
		}
		renderJSON(YUtils.ligerGridData(ConstructionProject.toJSON(constPors), total));
	}
	
	/**
	 * 审核处理
	 * @param id ConstrucationProject id
	 * @param pass true 审核通过，false 审核失败
	 */
	public static void verifyOpreate(long id ,boolean pass){
		ConstructionProject cp = ConstructionProject.findById(id);
		if(cp.verifyByHead()){
			renderText("本部已审核");
		}
		if(roleIds.contains("3")){//分公司审核
			cp.verifyState= pass?1:-1;
			cp.deptVerifyDate = new Date();
			cp.deptVerifyUser  = connectedUser();
			cp.save();
		}else if(roleIds.contains("5")){//本地审核
			cp.verifyState= pass?2:-2;
			cp.locationVerifyDate  =new Date();
			cp.locationVerifyUser = connectedUser();
			ProjectVisualizeReport.updAllowModify(cp.pvReprots,1);
			cp.save();
		}else{
			renderText("无权限审核");
		}
		ok();
	}
	
}
